<?php
/**
 * 供应商模块
 *
 * @author    wangmx <wangmx@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
namespace App\Services\SupplierService;

use DB;
use Illuminate\Http\Request;

class SupplierService
{
    /**
     * 创建供应商
     */
    public function createSupplier(Request $request)
    {
        $supplier_name = trim($request->get('supplier_name'));
        $supplier_contacts_name = trim($request->get('supplier_contacts_name'));
        $supplier_mobile = trim($request->get('supplier_mobile'));
        $supplier_type = $request->get('supplier_type');
        $remark = $request->get('remark');
        $template_ids = $request->get('template_ids');
        if (count($template_ids) == 0) {
            $data = array('message' => '请选择产品模板', 'result' => (Object) array(), 'state' => 'M01002');
            return $data;
        }
        $template_ids_string = implode(",", $template_ids);
        $validate_supplier = DB::table('suppliers')
            ->where(['supplier_name' => $supplier_name, 'state' => 1])
            ->first();
        //验证供应商名称是否存在
        if ($validate_supplier != null) {
            $data = array('message' => '已存在供应商名称', 'result' => (Object) array(), 'state' => 'M01001');
            return $data;
        }

        DB::table('suppliers')->insert(
            [
                'supplier_name' => $supplier_name,
                'supplier_contacts_name' => $supplier_contacts_name,
                'supplier_mobile' => $supplier_mobile,
                'supplier_type' => $supplier_type,
                'remark' => $remark,
                'template_ids' => $template_ids_string,
                'state' => 1,
                'created_at' => time(),
                'updated_at' => time(),
            ]
        );
        $data = array('message' => '', 'result' => (Object) array(), 'state' => 'M00000');
        return $data;
    }

    /**
     * @param Request $request
     * @return array
     * 编辑信息
     */
    public function edit(Request $request)
    {
        $supplier_id = $request->get('supplier_id');
        $supplier_name = trim($request->get('supplier_name'));
        $supplier_contacts_name = trim($request->get('supplier_contacts_name'));
        $supplier_mobile = trim($request->get('supplier_mobile'));
        $supplier_type = $request->get('supplier_type');
        $remark = $request->get('remark');
        $template_ids = $request->get('template_ids');
        if (count($template_ids) == 0) {
            $data = array('message' => '请选择产品模板', 'result' => (Object) array(), 'state' => 'M01002');
            return $data;
        }
        $template_ids_string = implode(",", $template_ids);
        $validate_supplier = DB::table('suppliers')
            ->whereNotIn('supplier_id', [$supplier_id])
            ->where(['supplier_name' => $supplier_name, 'state' => 1])
            ->first();

        //验证供应商名称是否存在
        if ($validate_supplier != null) {
            $data = array('message' => '已存在供应商名称', 'result' => (Object) array(), 'state' => 'M01001');
            return $data;
        }

        DB::table('suppliers')->where('supplier_id', $supplier_id)->update(
            [
                'supplier_name' => $supplier_name,
                'supplier_contacts_name' => $supplier_contacts_name,
                'supplier_mobile' => $supplier_mobile,
                'supplier_type' => $supplier_type,
                'remark' => $remark,
                'template_ids' => $template_ids_string,
                'state' => 1,
                'updated_at' => time(),
            ]
        );
        $data = array('message' => '', 'result' => (Object) array(), 'state' => 'M00000');
        return $data;
    }
}
